package com.lanou.dllo.findfood.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.dllo.findfood.MainActivity;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.tools.StringUtil;
import com.lanou.dllo.findfood.tools.TimeUtil;
import com.lanou.dllo.findfood.tools.ToastCommon;

import java.io.FileNotFoundException;

/**
 * Created by dllo on 15/11/7.
 */
public class SendActivity extends Activity implements View.OnClickListener {
    private ImageView sendIv,sendBack;
    private EditText sendText,dish_Name,tag;
    private String name,text,tags;
private Bitmap bitmap;
    private TextView send_disch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send);
        initView();
        getImage();
    }

  private  void   getImage(){
      Intent intent = getIntent();
      bitmap =intent.getParcelableExtra("Image");
      sendIv.setImageBitmap(bitmap);

    }
 private void  initView(){
     sendIv = (ImageView) findViewById(R.id.sendIv);
     sendBack = (ImageView) findViewById(R.id.sendBack);
     sendBack.setOnClickListener(this);
     sendText = (EditText) findViewById(R.id.sendText);
     dish_Name = (EditText) findViewById(R.id.dish_Name);
     send_disch = (TextView) findViewById(R.id.send_disch);
     send_disch.setOnClickListener(this);
     tag = (EditText) findViewById(R.id.tag);

     name = dish_Name.getText().toString();
     text = sendText.getText().toString();
     tags = tag.getText().toString();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sendBack:
                finish();

                break;
            case R.id.send_disch:
                if (dish_Name.getText().length()==0){
                    ToastCommon.createToastConfig().ToastShow(this, (ViewGroup) findViewById(R.id.toast_layout_root),
                            "菜名不能为空");
                }else
                {

                    showAlerterDialog();
                }

                break;

            default:
                break;
        }
    }

    private void showAlerterDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("发布");
        builder.setIcon(R.mipmap.app_icon_longin_user);
        name = dish_Name.getText().toString();
        tags = tag.getText().toString();
        text = sendText.getText().toString();
        builder.setMessage("即将发布,确定您的内容不修改了吗");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent in = new Intent(SendActivity.this, SharchedActivity.class);
                in.putExtra("sendText",text);//在跳转的时候传值到下一个页面
                in.putExtra("sendImage",bitmap);
                in.putExtra("dish_Name",name);//菜名发送了
                in.putExtra("tag",tags);//想一想如何把标签添加到文本中
                in.putExtra("time",TimeUtil.getCurrentTime("yyyy-MM-dd "));
                startActivityForResult(in, 800);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                //  Toast.makeText(getActivity(),"您取消了本次登录",Toast.LENGTH_SHORT).show();

            }
        });
        builder.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //传递图片
        if (resultCode == RESULT_OK) {
            Uri uri = data.getData();

            ContentResolver contentResolver = this.getContentResolver();
            try {
                BitmapFactory.Options opts = new BitmapFactory.Options();
                opts.inSampleSize = 4;
                bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri), null, opts);
                ImageView imageView = (ImageView) findViewById(R.id.sendIv);
                /* 将Bitmap设定到ImageView */
                imageView.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {

            }
        }
    }
}
